package com.jieyang.oa.controller.person_manager;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.jieyang.oa.controller.BaseServlet;
import com.jieyang.oa.pojo.Vo.EmpInfoVo;
import com.jieyang.oa.pojo.entity.Employee;
import com.jieyang.oa.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author 29276
 * @version 1.0
 * @description: 员工管理
 * @date 2021/8/15 18:13
 */
@WebServlet("/empServlet")
public class EmployeeServlet extends BaseServlet {

    @Autowired
    private EmployeeService employeeService;

    /**
     * @Author 2927624517@qq.com
     * @Description 查询所有管理人员
     * @Date 2021/8/28 10:50
     * @Param [req, resp]
     * @return void
    **/
    public void empLeader(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Integer emptyPe = Integer.valueOf(req.getParameter("emptyPe"));
        List<Employee> employees = employeeService.selectLeader(emptyPe);
        resp.getWriter().write(new Gson().toJson(employees));

    }

    /**
     * @Author 2927624517@qq.com
     * @Description 新增员工
     * @Date 2021/8/28 10:51
     * @Param [req, resp]
     * @return void
    **/
    public void empAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        resp.setContentType("text/html;charset=utf-8");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        String empId = req.getParameter("empId");
        Long deptNo = Long.valueOf(req.getParameter("deptNo"));
        Integer posId = Integer.parseInt(req.getParameter("posId"));
        String mgrId = req.getParameter("mgrId");
        String realName = req.getParameter("realName");
        String sex = req.getParameter("sex");
        String birthdate = req.getParameter("birthdate");
        String hireDate = req.getParameter("hireDate");
        String date = req.getParameter("leaveDate");
        String leaveDate;
        if (date.equals("")) {
            leaveDate = null;
        }
        else {
            leaveDate = String.valueOf(sdf.parse(date));
        }
        String onDuty = req.getParameter("onDuty");
        Integer emptyPe = Integer.parseInt(req.getParameter("emptyPe"));
        String phone = req.getParameter("phone");
        String qq = req.getParameter("qq");
        String emercontactPerson = req.getParameter("emercontactPerson");
        String idCard = req.getParameter("idCard");

        Employee employee = new Employee();
        employee.setEmpId(empId);
        employee.setDeptNo(deptNo);
        employee.setPosId(posId);
        employee.setMgrId(mgrId);
        employee.setRealName(realName);
        employee.setSex(sex);
        employee.setBirthdate(birthdate);
        employee.setHireDate(hireDate);
        employee.setLeaveDate(leaveDate);
        employee.setOnDuty(onDuty);
        employee.setEmptyPe(emptyPe);
        employee.setPhone(phone);
        employee.setEmail(qq);
        employee.setEmercontactPerson(emercontactPerson);
        employee.setIdCard(idCard);
        employee.setPassword("123456");

        Integer insert = employeeService.insert(employee);
        if (insert == 0) {
            resp.getWriter().write("false");
        } else {
            resp.getWriter().write("true");
        }
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 更新员工信息（查询员工旧信息并返回）
     * @Date 2021/8/28 10:52
     * @Param [req, resp]
     * @return void
    **/
    public void empUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        resp.setContentType("text/html;charset=utf-8");
        String empId = req.getParameter("empId");
        Employee employee = employeeService.selectEmpByEmpId(empId);

        req.setAttribute("emp", employee);
        req.getRequestDispatcher("personManager/empUpdate.jsp").forward(req, resp);
    }


    /**
     * @Author 2927624517@qq.com
     * @Description 更新员工信息（确认员工更新的信息）
     * @Date 2021/8/28 10:53
     * @Param [req, resp]
     * @return void
    **/
    public void empConfirmUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        resp.setContentType("text/html;charset=utf-8");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        String empId = req.getParameter("empId");
        Long deptNo = Long.valueOf(req.getParameter("deptNo"));
        Integer posId = Integer.parseInt(req.getParameter("posId"));
        String mgrId = req.getParameter("mgrId");
        String realName = req.getParameter("realName");
        String birthdate = req.getParameter("birthdate");
        String hireDate = req.getParameter("hireDate");
        String date = req.getParameter("leaveDate");
        String leaveDate;
        if (date.equals("")) {
            leaveDate = null;
        }
        else {
            leaveDate = String.valueOf(sdf.parse(date));
        }

        String phone = req.getParameter("phone");
        String qq = req.getParameter("qq");
        String idCard = req.getParameter("idCard");

        Employee employee = new Employee();
        employee.setEmpId(empId);
        employee.setDeptNo(deptNo);
        employee.setPosId(posId);
        employee.setMgrId(mgrId);
        employee.setRealName(realName);
        employee.setBirthdate(birthdate);
        employee.setHireDate(hireDate);
        employee.setLeaveDate(leaveDate);
        employee.setPhone(phone);
        employee.setEmail(qq);
        Employee emp = (Employee) req.getSession().getAttribute("emp");
        employee.setEmercontactPerson(emp.getEmercontactPerson());
        employee.setIdCard(idCard);

        Integer insert = employeeService.update(employee);
        if (insert == 0) {
            resp.getWriter().write("false");
        } else {
            resp.getWriter().write("true");
        }
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 删除员工
     * @Date 2021/8/28 10:54
     * @Param [req, resp]
     * @return void
    **/
    public void deleteEmp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String empId = req.getParameter("empId");
        Integer delete = employeeService.delete(empId);
        if (delete == 0) {
            resp.getWriter().write("false");
        } else {
            resp.getWriter().write("true");
        }
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 初始化员工密码
     * @Date 2021/8/28 10:54
     * @Param [req, resp]
     * @return void
    **/
    public void initPwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String empId = req.getParameter("empId");
        Integer delete = employeeService.initPwd(empId);
        System.out.println("=================");
        System.out.println(empId);
        if (delete == 0) {
            resp.getWriter().write("false");
        } else {
            resp.getWriter().write("true");
        }
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 查询所有员工
     * @Date 2021/8/28 10:55
     * @Param [req, resp]
     * @return void
    **/
    public void empList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        resp.setContentType("text/html;charset=utf-8");
        int pageNum = Integer.parseInt(req.getParameter("pageNum"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        String realName = req.getParameter("realName");
        Long deptNo;
        String no = req.getParameter("deptNo");
        if (no != null) {
            if ("".equals(no)) {
                deptNo = null;
            } else {
                deptNo = Long.valueOf(no);
            }

        } else {
            deptNo = null;
        }
        String onDuty = req.getParameter("onDuty");
        String hireDate;
        String date = req.getParameter("hireDate");
        if (date != null) {
            if ("".equals(date)) {
                hireDate = null;
            } else {
                hireDate = String.valueOf(sdf.parse(date));
            }

        } else {
            hireDate = null;
        }
        Employee employee = new Employee();
        employee.setRealName(realName);
        employee.setDeptNo(deptNo);
        employee.setOnDuty(onDuty);
        employee.setHireDate(hireDate);

        PageInfo<Employee> employeePageInfo = employeeService.selectByPage(pageNum, pageSize, employee);
        resp.getWriter().write(new Gson().toJson(employeePageInfo));
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 查询某位员工
     * @Date 2021/8/28 10:55
     * @Param [req, resp]
     * @return void
    **/
    public void selectEmp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        resp.setContentType("text/html;charset=utf-8");
        String empId = req.getParameter("empId");

        EmpInfoVo employee = employeeService.selectByEmpId(empId);

        req.setAttribute("emp", employee);
        req.getRequestDispatcher("myInformation/myInfo.jsp").forward(req, resp);

    }
}